package com.ruoyi.flowable.listener.scrap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquScrap;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 报废-3-1监听：院长额外审批
 */
@Slf4j
@Component("scrapListener_3_1")
public class ScrapListener_3_1 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //院长额外审批
            HashMap<String, Object> deanMap = new HashMap<>();
            String flag7 = execution.getVariable("flag7", String.class);
            deanMap.put("主管副院长审批", flag7);

            EquScrap equScrap = execution.getVariable("equScrap", EquScrap.class);

            HashMap<String, Object> approvalSummaryMap = objectMapper.readValue(equScrap.getApprovalSummary(), new TypeReference<HashMap<String, Object>>() {});

            approvalSummaryMap.put("院长额外审批", deanMap);
            String approvalSummary = objectMapper.writeValueAsString(approvalSummaryMap);

            equScrap.setApprovalSummary(approvalSummary);

            execution.setVariable("equScrap", equScrap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
